<?php
//>> 激活游戏

if(!defined('IN_DISCUZ')) exit('Access Denied!');

function httpGet($url, &$err) {
    //初始化
    $ch = curl_init();

    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    if (!$output) {
        $err = curl_error($ch);
    }

    //释放curl句柄
    curl_close($ch);

    return $output;
}

// 插件配置
$plugin_conf = $_G['cache']['plugin']['mangos'];

// 大芒果接口
$mangos_quapi = $plugin_conf['mangos_quapi'];
$mangos_cuapi = $plugin_conf['mangos_cuapi'];

if ($_REQUEST['active'] == 1) {
    $email = $_REQUEST['email'];
    $password = $_REQUEST['password'];
    $password2 = $_REQUEST['password2'];

    if (empty($password)) {
        $error = '请输入密码';
    } else if ($password != $password2) {
        $error = '两次输入密码不一致';
    } else {
        $password = strtoupper($_G['username']) . ':' . strtoupper($password);
        $password = sha1($password);

        $url = sprintf('%s&account=%s&password=%s&email=%s', $mangos_cuapi, $_G['username'], $password, $email);
        // 创建帐号
        $httpErr = '';
        $res = httpGet($url, $httpErr);
        if (!$res) {
            $error = $httpErr;
        } else {
            $res = json_decode($res, true);

            if ($res['code'] != 0) {
                $error = $res['desc'];
            } else {
                $error = '<h1>激活成功</h1>';
            }
        }
    }
} else {
    // 检查角色是否存在
    $httpErr = '';
    $res = httpGet($mangos_quapi . '&account=' . $_G['username'], $httpErr);
    if (!$res) {
        $error = $httpErr;
    } else {
        $res = json_decode($res, true);

        if ($res['code'] != 0) {
            $error = $res['desc'];
        } else {
            if ($res['data']) {
                $exists = true;
            }
        }
    }
}